﻿#pragma once

#include "Render/Texture.h"

// lib
#include <glm/glm.hpp>

// std
#include <unordered_map>
#include <string>

class CORE_API Atlas : public Texture
{
public:
  Atlas(uint32_t target = GL_TEXTURE_2D,
        uint32_t internalFormat = GL_RGBA,
        uint32_t format = GL_RGBA,
        uint32_t type = GL_UNSIGNED_BYTE,
        bool generateMipMap = true,
        int32_t maxLod = 1000);

  Atlas(
    const std::string &directoryPath, 
    int atlasWidth, int atlasHeight, 
    uint32_t target = GL_TEXTURE_2D, 
    uint32_t internalFormat = GL_RGBA, 
    uint32_t format = GL_RGBA, 
    uint32_t type = GL_UNSIGNED_BYTE, 
    bool generateMipMap = true, 
    int32_t maxLod = 1000);

  Atlas(const std::string &jsonPath);

  void CreateFromDirectory(const std::string &directoryPath, int atlasWidth, int atlasHeight);
  void CreateFromJson(const std::string &jsonPath);
  std::pair<glm::ivec2, int> GetTextureIndex(const std::string &textureName) const;

  const int TileSize = 16;

private:
  bool FindEmpityPosition(
      uint8_t *atlasPixels,
      int atlasWidth, int atlasHeight,
      int texWidth, int texHeight,
      int &x, int &y);
  
  std::unordered_map<std::string, std::pair<glm::ivec2, int>> m_textureUVMap;
  std::string m_prefix = "";
};